package com.eliza.music.model.entity

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.room.TypeConverter
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.ByteArrayOutputStream

class Converter {

    @TypeConverter
    fun fromBitmap(bitmap: Bitmap): ByteArray {
        val outputStream = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
        return outputStream.toByteArray()
    }

    @TypeConverter
    fun toBitmap(byteArray: ByteArray): Bitmap {
        return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
    }

    @TypeConverter
    fun jsonMarshalInt(byteArray: Array<Int>): String {
        return Json.encodeToString(byteArray)
    }

    @TypeConverter
    fun jsonMarshalStr(byteArray: Array<String>): String {
        return Json.encodeToString(byteArray)
    }

    @TypeConverter
    fun jsonUnMarshalInt(byteArray: String): Array<Int> {
        return Json.decodeFromString<Array<Int>>(byteArray)
    }

    @TypeConverter
    fun jsonUnMarshalStr(byteArray: String): Array<String> {
        return Json.decodeFromString<Array<String>>(byteArray)
    }
}
